// pages/vip/vip.js
const $ = require('../../utils/utils.js')
const config = require('../../config')
const hostUrl = require('../../config').hostUrl
Page({

    /**
     * 页面的初始数据
     */
    data: {
        vip_list: [],
        tip_content: '您的VIP特权将在2020年10月10日内截止，点击下列按钮为您的特权续费获取更多的特权时间。',
        selectImg: '',
        vipItemNor: 'vipItem-nor',
        vipItemSel: 'vipItem-sel',
        vipItemTitleSel: 'vipItem-title-sel',
        vipItemTitleNor: 'vipItem-title',
        vip_amount: '',
        vip_type: '1',
        sel_vip: {},
        payImgSrc: '../../img/vipIconImg.png',
        payImgSrcNor: '../../img/selNor.png',
        cashIndex: true,
        showCoupon: false,
        cash_flag: 1,
        cash_amount: '0',
        extra_amount: 0,
        pay_amount: '0',
        coupon_id: '',
        coupon_type: '',
        coupon_discount: '',
        coupon_amount: '',
        couponList: [],
        couponListNor: [],
        oper: '1'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let data = {
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken'),
        }
        let vipurl = $.getUrl(config.getVIPAmountList, data)
        $.get(vipurl)
            .then(data => {
                this.setData({
                    vip_list: data.detail.list,
                    vip_type: data.detail.list[0].vip_type,
                    tip_content: data.detail.tip_content,
                    sel_vip: data.detail.list[0],
                    selectImg: data.detail.list[0].vip_imgurl,
                    cash_flag: data.detail.cash_flag,
                    extra_amount: data.detail.list[0].extra_amount,
                    cash_amount: data.detail.cash_amount,
                    pay_amount: data.detail.list[0].pay_amount,
                    coupon_id: data.detail.list[0].coupon_id,
                    coupon_type: data.detail.list[0].coupon_type,
                    coupon_discount: data.detail.list[0].coupon_discount,
                    coupon_amount: data.detail.list[0].coupon_amount,
                    vip_amount: data.detail.list[0].vip_amount,
                })

                let cash_amount = Number(data.detail.cash_amount)
                if(cash_amount <= 0){
                    this.setData({
                        cashIndex:false,
                    })
                }
            })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    confirmCoupon: function () {
        wx.showModal({
            title: '提示',
            content: '您当前没有可用的优惠券',
            showCancel: false,
            confirmColor: '#FF3B30',
        })
    },

    // 优惠券列表
    showCouponList: function () {

        let data = {
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken'),
            oper: this.data.oper,
            amount: this.data.vip_amount
        }
        let url = $.getUrl(config.getCouponList, data)
        $.get(url).then(data => {
            this.setData({
                showCoupon: true,
                couponList: [],
                couponListNor: []
            })
            console.log('#vip_showCouponList', data)
            let list = data.items
            let couponList = this.data.couponList
            let couponListNor = this.data.couponListNor
            for (var i = 0; i < list.length; i++) {
                if (list[i].avaiable == 1) {
                    couponList.push(list[i])
                } else {
                    couponListNor.push(list[i])
                }
            }
            this.setData({
                // showCoupon: true,
                couponList: couponList,
                couponListNor: couponListNor
            })
        })

    },

    shut_shadow: function () {
        this.setData({
            showCoupon: false
        })
    },
    // 选择优惠券并且计算金额
    selCoupon: function (e) {
        console.log('vip_selCoupon', e)
        let couponId = e.currentTarget.dataset.id
        let data = {}
        if (this.data.coupon_id == couponId){
            this.setData({
                coupon_id: 'empty'
            }) 
        }else{
            this.setData({
                coupon_id: couponId
            })
        }
        
        this.calcPayAmount(this.data.coupon_id)
    },

    // 优惠券计算金额
    calcPayAmount: function (couponId) {
        wx.showLoading({
            title: '正在加载中...',
            mask:true
        })
        let that = this
        let cashIndex = this.data.cashIndex
        let data = {
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken'),
            coupon_id: couponId,
            oper: this.data.oper,
            amount: this.data.vip_amount,
        }
        if (cashIndex) {
            data.cash_amount = this.data.cash_amount
        } 
        $.post(config.calcPayAmount, data).then(data => {
            wx.hideLoading()
            console.log('#vip_selCoupon_data--', data)
            if (data.code == 0) {
                that.setData({
                    coupon_discount: data.detail.coupon_discount,
                    coupon_id: data.detail.coupon_id,
                    coupon_type: data.detail.coupon_type,
                    pay_amount: data.detail.pay_amount,
                    coupon_amount: data.detail.coupon_amount,
                    extra_amount: data.detail.extra_amount
                })
            }

        }).catch(error=>{
            wx.hideLoading()
        })
    },

    // 切换购买年/月卡
    tapVipItem: function (e) {
        console.log('#vip_tapVipItem', this.data.extra_amount)
        let vip_type = e.target.dataset.vip.vip_type
        if (vip_type == this.data.sel_vip.vip_type) {
            return;
        } else {
            this.setData({
                sel_vip: e.target.dataset.vip,
                selectImg: e.target.dataset.vip.vip_imgurl,
                coupon_id: e.target.dataset.vip.coupon_id,
                coupon_type: e.target.dataset.vip.coupon_type,
                coupon_discount: e.target.dataset.vip.coupon_discount,
                coupon_amount: e.target.dataset.vip.coupon_amount,
                pay_amount: e.target.dataset.vip.pay_amount,
                vip_amount: e.target.dataset.vip.vip_amount,
                vip_type: e.target.dataset.vip.vip_type
            })
        }
        if (vip_type == '1') {
            this.setData({
                oper: '1'
            })
        } else {
            this.setData({
                oper: '2'
            })
        }

        this.setData({
            couponList:[]
        })
        this.calcPayAmount(e.target.dataset.vip.coupon_id)
    },

    //选择余额支付 
    cashChargeType: function (e) {
        console.log('#vip_cashChargeType')
        let cash_amount = this.data.cash_amount
        let cashIndex = this.data.cashIndex
        let couponId = this.data.coupon_id
        this.setData({
            cashIndex: !cashIndex,
            extra_amount:"0.00"
        })
        this.calcPayAmount(couponId)
    },

    showTapToast: function(){
        $.showToast("您的余额不足")
    },

    // 选择微信支付
    wechatChargeType: function (e) {
        console.log('#vip_wechatChargeType')
        let cashIndex = this.data.cashIndex
        let couponId = this.data.coupon_id
        this.setData({
            cashIndex: !cashIndex
        })
        console.log('cashIndex----', this.data.cashIndex)
        this.calcPayAmount(couponId)
    },

    toRequsetOrder: function () {

        let data = {
            chargetype: 1,
            chargeamount: this.data.vip_amount,
            pay_amount: this.data.pay_amount,
            userid: wx.getStorageSync('userid'),
            usertoken: wx.getStorageSync('usertoken'),
            coupon_id: this.data.coupon_id,
            extra_amount: this.data.extra_amount,
            vip_type: this.data.vip_type
        }
        console.log("#toRequsetOrder",data)
        wx.showLoading({
            title: '正在请求支付',
            mask: true
        })
        $.post(config.paymentVIPCard, data).then(data => {
            console.log('#vip_toRequsetOrder___data', data)
            var payid = data.detail.prepayId;
            console.log("prepayId:" + payid);
            var payargs = data.payargs;
            wx.hideLoading();
            if (data.code == '0') {
                if (data.detail.payFlag == 1) {
                    wx.requestPayment({
                        timeStamp: data.detail.timeStamp,
                        nonceStr: data.detail.nonceStr,
                        package: data.detail.pkg,
                        signType: data.detail.signType,
                        paySign: data.detail.sign,
                        'success': function (res) {
                            //支付成功跳转页面
                            wx.hideLoading();
                            console.log("topup success");
                            $.goto(config.page.getVipSuccess,true)
                        },
                        'fail': function (res) {
                            
                            let data = {
                                userid: wx.getStorageSync('userid'),
                                usertoken: wx.getStorageSync('usertoken'),
                            }
                            let url = config.cancelPayment
                            $.post(url,data).then(data=>{
                            })
                            
                            //支付失败跳转页面
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none',
                                duration: 2000
                            });

                        }
                    });
                } else if (data.detail.payFlag == 0) {
                    $.goto(config.page.getVipSuccess, true)
                }


            } else {
                wx.showModal({
                    title: '支付错误！',
                    content: data.message,
                })
            }
        })

    }
})
